// pages/groupSpace/publish/publish.js
const app = getApp();
const util = require('../../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CarGroupID: 0,
    imgList: [],
    //1：随笔 2：游记 3.求助 4心情
    type: ['随笔', '游记', '求助', '心情'],
    currentIndex: 0,
    commentDetail: '',
    fileUploadIndex: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      CarGroupID: options.CarGroupID || 1
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  textareaAInput: function(e) {
    this.data.commentDetail = e.detail.value;
  },
  /**
   * 发布
   */
  addGroupComment: function() {
    let self = this;
    if (util.isNullOrEmpty(self.data.commentDetail)) {
      wx.showToast({
        title: '请填写帖子内容',
        icon: 'none'
      });
      return;
    }

    let data = {
      Detail: self.data.commentDetail,
      CommentSection: self.data.currentIndex + 1,
      CarGroupID: self.data.CarGroupID,
      UserID: app.globalData.userInfo.UserID,
      OpenID: app.globalData.userInfo.OpenID,
      CommentImgs: []
    };

    if (self.data.imgList.length > 0) {
      self.uploadImage(self.data.imgList[self.data.fileUploadIndex], data);
    } else {
      self.addGroupCommentNoImg(data);
    }
  },
  uploadImage: function(filePath, data) {
    let self = this;
    wx.uploadFile({
      url: app.globalData.apiRootUrl + '/Common/FileUpload',
      filePath: filePath,
      name: 'file',
      formData: {},
      success: function(res) {
        //console.log(res)
        let rdata = JSON.parse(res.data) ;
        if (rdata.status == 0) {
          data.CommentImgs.push({
            ImageUrl: rdata.data,
            ImageSort: self.data.fileUploadIndex
          });
          //console.log(data)
        }
        if (self.data.fileUploadIndex < self.data.imgList.length - 1) {
          self.data.fileUploadIndex++;
          self.uploadImage(self.data.imgList[self.data.fileUploadIndex], data);
        } else {
          //console.log(data)
          self.addGroupCommentNoImg(data);
        }
      },
      fail: function(e) {
        wx.showToast({
          title: '上传图片失败',
          icon: 'none'
        })
      }
    })
  },
  addGroupCommentNoImg: function(data) {
    let self = this;
    let url = app.globalData.apiRootUrl + '/GroupComment/AddComment';
    app.httpRequest({
      url: url,
      showLoading: true,
      method: "POST",
      data: data,
      success: function(res) {
        let rdata = res.data;
        if (rdata.status == 0) {
          wx.navigateBack();
        } else {
          wx.showToast({
            title: rdata.message,
            icon: "none"
          })
        }
      }
    });
  },
  /**
   * 选择图片
   */
  ChooseImage() {
    wx.chooseImage({
      count: 4, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        if (this.data.imgList.length != 0) {
          this.setData({
            imgList: this.data.imgList.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgList: res.tempFilePaths
          })
        }
      }
    });
  },
  /**
   * 图片预览
   */
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  /**
   * 删除图片
   */
  DelImg(e) {
    wx.showModal({
      title: '提示',
      content: '确定要删除这个图片吗？',
      cancelText: '再想想',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          this.data.imgList.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList: this.data.imgList
          })
        }
      }
    })
  },
  /**
   * 选择类型
   */
  tabTap: function(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      currentIndex: index
    })
  }
})